package com.talkweb.ncfw.security.permission;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.InitializingBean;

import com.talkweb.ncfw.component.rights.entity.Func;
import com.talkweb.ncfw.component.rights.entity.RoleFunc;
import com.talkweb.ncfw.component.rights.service.IFuncManager;
import com.talkweb.ncfw.component.rights.service.IRightManager;
import com.talkweb.ncfw.data.IUserSession;
import com.talkweb.ncfw.data.Page;
import com.talkweb.ncfw.utils.AssertUtils;
import com.talkweb.ncfw.utils.StringUtils;

/**
 * <p>
 * 文件名称: FuncPermissionManagerImpl.java
 * </p>
 * <p>
 * 文件描述: 本类描述
 * </p>
 * <p>
 * 版权所有: 版权所有(C)2010
 * </p>
 * <p>
 * 内容摘要: 简要描述本文件的内容，包括主要模块、函数及能的说明
 * </p>
 * <p>
 * 其他说明: 其它内容的说明
 * </p>
 * <p>
 * 完成日期: 2011-10-23
 * </p>
 * <p>
 * 修改记录1:
 * </p>
 * 
 * <pre>
 *    修改日期:
 *    修 改 人:
 *    修改内容:
 * </pre>
 * <p>
 * 修改记录2：…
 * </p>
 * 
 * @author Wuqingming
 */
public class FuncPermissionManagerImpl implements IFuncPermissionManager, InitializingBean {
	private static Log log = LogFactory.getLog(FuncPermissionManagerImpl.class);
	private PermissionResolverChain<IFuncPermissionResolver> funcPermissionResolverChain;
	private IFuncPermissionStore funcPermissionStore;
	private IRightManager rightManager;
	private IFuncManager funcManager;

	public PermissionResolverChain<IFuncPermissionResolver> getFuncPermissionResolverChain() {
		return funcPermissionResolverChain;
	}

	public void setFuncPermissionResolverChain(
			PermissionResolverChain<IFuncPermissionResolver> funcPermissionResolverChain) {
		this.funcPermissionResolverChain = funcPermissionResolverChain;
	}

	public IFuncPermissionStore getFuncPermissionStore() {
		return funcPermissionStore;
	}

	public void setFuncPermissionStore(IFuncPermissionStore funcPermissionStore) {
		this.funcPermissionStore = funcPermissionStore;
	}

	public IRightManager getRightManager() {
		return rightManager;
	}

	public void setRightManager(IRightManager rightManager) {
		this.rightManager = rightManager;
	}

	public IFuncManager getFuncManager() {
		return funcManager;
	}

	public void setFuncManager(IFuncManager funcManager) {
		this.funcManager = funcManager;
	}

	public boolean hasFuncPermission(IUserSession userSession, String funccode) {
		log.info("begin to excute hasFuncPermission, funccode:[" + funccode+ "]");
		if (funcPermissionResolverChain == null) {
			log.warn("funcPermissionResolverChain is null.");
			return true;
		}
		if (StringUtils.isEmpty(funccode)) {
			log.info("funccode is empty!");
			return false;
		}
		if (userSession == null) {
			log.info("userSession is null.");
			return false;
		}
		List<IFuncPermissionResolver> funcPermissionResolvers = funcPermissionResolverChain.getResolvers();
		if (funcPermissionResolvers == null || funcPermissionResolvers.size() == 0) {
			log.warn("funcPermissionResolverChain is null.");
			return true;
		}
		for (IFuncPermissionResolver funcPermissionResolver : funcPermissionResolvers) {
			if (funcPermissionResolver.hasFuncPermission(userSession, funccode)) {
				return true;
			}
		}
		return false;
	}

	public void addFuncPermissions(FuncPermission... funcPermissions) {
		this.funcPermissionStore.saveFuncPermissions(funcPermissions);
	}

	public void removeFuncPermissions(String... funccodes) {
		this.funcPermissionStore.removeFuncPermissions(funccodes);
	}

	public void removeAllFuncPermission() {
		this.funcPermissionStore.removeAll();
	}

	public void afterPropertiesSet() throws Exception {
		AssertUtils.notNull(funcPermissionStore, "funcPermissionStore can not be null.");
		AssertUtils.notNull(rightManager, "rightManager can not be null.");
		AssertUtils.notNull(funcManager, "funcManager can not be null.");
		initFuncPermissions();
	}
	
	/**
	 * 
	 * @author：Wuqingming 	        
	 * @date：2011-10-23
	 * @Description：初始化功能许可信息
	 */
	private void initFuncPermissions () throws Exception {
		/** 获取所有的功能 */
		Page<Func> funcPage = new Page<Func>();
		funcPage.setIfPage(false);
		List<Func> funcList = this.funcManager.findFunc(funcPage, null).getResult();
		if (funcList == null || funcList.size() == 0) {
			return;
		}
		
		/** 获取所有的角色功能关系 */
		List<RoleFunc> roleFucList = this.rightManager.findAllRoleFunc();
		
		/** 组装功能许可信息 */
		FuncPermission[] funcPermissions = new FuncPermission[funcList.size()];
		for (int i = 0; i < funcList.size(); i++) {
			Func func = funcList.get(i);
			FuncPermission funcPermission = new FuncPermission();
			funcPermissions[i] = funcPermission;
			funcPermission.setFunccode(func.getFunccode());
			funcPermission.setFuncname(func.getFuncname());
			if (roleFucList == null || roleFucList.size() == 0) {
				continue;
			}
			List<String> roles = null;
			for (RoleFunc roleFunc : roleFucList) {
				if (StringUtils.equals(roleFunc.getFuncid(), func.getFuncid())) {
					if (roles == null) {
						roles = new ArrayList<String>();
					}
					roles.add(roleFunc.getUserroleId().toString());
				}
			}
			funcPermission.setRoles(roles);
		}
		
		/** 保存功能许可信息 */
		this.funcPermissionStore.saveFuncPermissions(funcPermissions);
	}

}
